package com.aixingfu.sports.MVP.presenter;

import android.util.Log;

import com.aixingfu.sports.Https.NetHome;
import com.aixingfu.sports.Https.NetListener;
import com.aixingfu.sports.MVP.contract.BuyChargeClassContract;
import com.aixingfu.sports.MVP.contract.SignUploadContract;
import com.aixingfu.sports.Model.BuyChargeClassModel;
import com.aixingfu.sports.Model.SignUploadModel;
import com.aixingfu.sports.Model.UserSignModel;
import com.orie.librarybase.BaseApplication;
import com.orie.librarybase.utils.UserUtils;

import java.io.File;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Author : 吕冬冬
 * Time : 2019.05.17 0011
 * Description : 购课签名上传
 */
public class SignUploadPresenter implements SignUploadContract.MyPresenter {

    SignUploadContract.MyView mView;
    @Override
    public void attachView(SignUploadContract.MyView view) {
        mView=view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }

    @Override
    public boolean isViewAttached() {
        return mView != null;
    }

    public void getSignUpload(File file,HashMap<String,String> map){
        mView.showLoading();
        String token = UserUtils.getToken(BaseApplication.getContext());
        NetHome.loadData(NetHome.getApi(mView.getMyContext()).getSignUpload(token,filesToMultipartBodyParts(file),RequestBody.create(MediaType.parse("text/plain"),map.get("type"))), new NetListener<SignUploadModel>() {
            @Override
            public void onSuccess(SignUploadModel data) {
                mView.setSignUpload(data);
            }

            @Override
            public void onFailed(int errCode, String errMsg) {
                Log.i("打印数据", "onFailed: "+errCode+"  "+errMsg);
            }
            @Override
            public void onComplete() {
                mView.hideLoading();
            }
        });
    }
    public MultipartBody.Part filesToMultipartBodyParts(File files){
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), files);
        return MultipartBody.Part.createFormData("file", files.getName(), requestBody);
    }
}
